package com.castle.fortress.admin.cms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.cms.dto.CmsSubjectDto;
import com.castle.fortress.admin.cms.entity.CmsSubjectEntity;
import com.castle.fortress.admin.cms.mapper.CmsSubjectMapper;
import com.castle.fortress.admin.cms.service.CmsSubjectService;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.respcode.BizErrorCode;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * cms专题配置表 服务实现类
 *
 * @author castle
 * @since 2022-03-07
 */
@Service
public class CmsSubjectServiceImpl extends ServiceImpl<CmsSubjectMapper, CmsSubjectEntity> implements CmsSubjectService {
    /**
    * 获取查询条件
    * @param cmsSubjectDto
    * @return
    */
    public QueryWrapper<CmsSubjectEntity> getWrapper(CmsSubjectDto cmsSubjectDto){
        QueryWrapper<CmsSubjectEntity> wrapper= new QueryWrapper();
        if(cmsSubjectDto != null){
            CmsSubjectEntity cmsSubjectEntity = ConvertUtil.transformObj(cmsSubjectDto,CmsSubjectEntity.class);
            wrapper.like(StrUtil.isNotEmpty(cmsSubjectEntity.getTitle()),"title",cmsSubjectEntity.getTitle());
            wrapper.like(StrUtil.isNotEmpty(cmsSubjectEntity.getKeywords()),"keywords",cmsSubjectEntity.getKeywords());
            wrapper.like(StrUtil.isNotEmpty(cmsSubjectEntity.getName()),"name",cmsSubjectEntity.getName());
            wrapper.eq(StrUtil.isNotEmpty(cmsSubjectEntity.getCode()),"code",cmsSubjectEntity.getCode());
            wrapper.eq(cmsSubjectEntity.getShowflag()!=null,"showflag",cmsSubjectEntity.getShowflag());
            wrapper.like(StrUtil.isNotEmpty(cmsSubjectEntity.getInfo()),"info",cmsSubjectEntity.getInfo());
        }
        wrapper.orderByAsc("sort");
        return wrapper;
    }


    @Override
    public IPage<CmsSubjectDto> pageCmsSubject(Page<CmsSubjectDto> page, CmsSubjectDto cmsSubjectDto) {
        QueryWrapper<CmsSubjectEntity> wrapper = getWrapper(cmsSubjectDto);
        Page<CmsSubjectEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<CmsSubjectEntity> cmsSubjectPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<CmsSubjectDto> pageDto = new Page(cmsSubjectPage.getCurrent(), cmsSubjectPage.getSize(),cmsSubjectPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(cmsSubjectPage.getRecords(),CmsSubjectDto.class));
        return pageDto;
    }


    @Override
    public List<CmsSubjectDto> listCmsSubject(CmsSubjectDto cmsSubjectDto){
        QueryWrapper<CmsSubjectEntity> wrapper = getWrapper(cmsSubjectDto);
        List<CmsSubjectEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,CmsSubjectDto.class);
    }

    @Override
    public RespBody checkColumnRepeat(CmsSubjectDto dto) {
        //校验code
        QueryWrapper<CmsSubjectEntity> queryWrapper=new QueryWrapper();
        queryWrapper.eq("code",dto.getCode());
        List<CmsSubjectEntity> list = baseMapper.selectList(queryWrapper);
        if(list != null && !list.isEmpty()){
            //新增
            if(dto.getId() == null){
                return RespBody.fail(BizErrorCode.CMS_CODE_EXIST_ERROR);
                //修改
            }else{
                for(CmsSubjectEntity entity:list){
                    if(!entity.getId().equals(dto.getId())){
                        return RespBody.fail(BizErrorCode.CMS_CODE_EXIST_ERROR);
                    }
                }
            }
        }
        return RespBody.data("校验通过");
    }

    @Override
    public List<CmsSubjectDto> cmsTagListCmsSpecial(Map<String, Object> map) {
        return ConvertUtil.transformObjList(baseMapper.cmsTagListCmsSpecial(map), CmsSubjectDto.class);
    }
}

